/*
 * Copyright 2015-2102 RonCoo(http://www.roncoo.com) Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.fastoo.common.exception;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BizException extends RuntimeException {

    private static final long serialVersionUID = -5875371379845226068L;

    private static final Logger logger = LoggerFactory.getLogger(BizException.class);

    public static final BizException DB_EERROR = new BizException(
            10040000, "数据库操作错误");

    /**
     * 数据库操作,insert返回0
     */
    public static final BizException DB_INSERT_RESULT_0 = new BizException(
            10040001, "数据库操作,insert返回0");

    /**
     * 数据库操作,insert返回0
     */
    public static final BizException DB_BATCH_INSERT_RESULT_0 = new BizException(
            1004000101, "数据库操作,insert返回0");

    /**
     * 数据库操作,update返回0
     */
    public static final BizException DB_UPDATE_RESULT_0 = new BizException(
            10040002, "数据库操作,update返回0");




    /**
     * 数据库操作,delete返回0
     */
    public static final BizException DB_DELETE_RESULT_0 = new BizException(
            10040003, "数据库操作,update返回0");

    /**
     * 数据库操作,select异常错误
     */
    public static final BizException DB_SELECT_ERROR = new BizException(
            10040004, "数据库操作,select异常错误");

    /**
     * 数据库操作,分页查询异常错误
     */
    public static final BizException DB_PAGE_ERROR = new BizException(
            1004000401, "数据库操作,select 分页查询异常错误");

    /**
     * 异常信息
     */
    protected String msg;

    /**
     * 具体异常码
     */
    protected int code;

    public BizException(int code, String msgFormat, Object... args) {
        super(String.format(msgFormat, args));
        this.code = code;
        this.msg = String.format(msgFormat, args);
    }

    public BizException() {
        super();
    }

    public BizException(String message, Throwable cause) {
        super(message, cause);
    }

    public BizException(Throwable cause) {
        super(cause);
    }

    public BizException(String message) {
        super(message);
    }

    public String getMsg() {
        return msg;
    }

    public int getCode() {
        return code;
    }

    /**
     * 实例化异常
     * 
     * @param msgFormat
     * @param args
     * @return
     */
    public BizException newInstance(String msgFormat, Object... args) {
        return new BizException(this.code, msgFormat, args).print();
    }

    public BizException print() {
        logger.error("==>BizException, code:" + this.code + ", msg:" + this.msg);
        return this;
    }

}
